<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Shenqing extends Controllers{

	public function index()
	{
	

	$pindex    = max(1, intval($_REQUEST['page']));

	$psize     = 20;

	
	
	$wheres = " AND uniacid=2 ";

	if(!empty($_REQUEST['id'])){

		$wheres.=" and id={$_REQUEST['id']}  ";
		$this->assign("id",$_REQUEST['id']);
		
	}

    if(!empty($_REQUEST['uid'])){

        $wheres.=" and user_id={$_REQUEST['uid']}  ";
		$this->assign("uid",$_REQUEST['uid']);

       
    }

    if(!empty($_REQUEST['uid2'])){

        $wheres.=" and user_id in (select uid from ims_xuan_zy_userup where (uid={$_REQUEST['uid2']} or suid={$_REQUEST['uid2']} or ssuid={$_REQUEST['uid2']})) ";
		$this->assign("uid2",$_REQUEST['uid2']);

    }

    if(!empty($_REQUEST['uid3'])){
    	$res ="select * from ims_xuan_zy_person where uid = {$_REQUEST['uid3']}";
	    $person=DB::query($res)[0];

        $wheres.=" and user_id in (select uid from ims_xuan_zy_userup where ssuid='{$_REQUEST['uid3']}') ";
		$this->assign("uid3",$_REQUEST['uid3']);


    }

	if(!empty($_REQUEST['name'])){

		$wheres.=" and hide_name LIKE '%".$_REQUEST['name']."%'";
				$this->assign("name",$_REQUEST['name']);

		
	}
	if(!empty($_REQUEST['mobile'])){

		$wheres.=" and mobile LIKE '%".$_REQUEST['mobile']."%'";	
						$this->assign("mobile",$_REQUEST['mobile']);

	}

	if(!empty($_REQUEST['idcard'])){

		$wheres.=" and idcard LIKE '%".$_REQUEST['idcard']."%'";
		$this->assign("idcard",$_REQUEST['idcard']);
		
	}
    if(!empty($_REQUEST['status'])){

        $wheres.=" and status={$_REQUEST['status']}  ";
		$this->assign("status",$_REQUEST['status']);

    }
    if(!empty($_REQUEST['bank_name'])){

        $wheres.=" and (bank_name LIKE '%".$_REQUEST['bank_name']."%' or bank_id LIKE '%".$_REQUEST['bank_name']."%')  ";
			$this->assign("bank_name",$_REQUEST['bank_name']);

    }

    if (!empty($_REQUEST['datelimit']['start']) && !empty($_REQUEST['datelimit']['end'])) {

        $starttime = strtotime($_REQUEST['datelimit']['start']);
        $endtime = strtotime($_REQUEST['datelimit']['end']);
        $wheres .= " AND create_time > '{$starttime}' AND create_time < '{$endtime}'";

		$this->assign("start",$_REQUEST['datelimit']['start']);
		$this->assign("end",$_REQUEST['datelimit']['end']);

  
    }



	$sql = "SELECT * FROM ims_xuan_zy_bankorder WHERE  1 {$wheres} order by id desc";

  
    $sql .= ' limit ' . (($pindex - 1) * $psize) . ',' . $psize;
	$list = DB::query($sql);
	foreach ($list as $key => &$value) {
		switch ($value['status']) {
			case '1':
				$value['status'] = "待确认";
				break;
			case '2':
				$value['status'] = "待再查";
				break;
			case '3':
				$value['status'] = "已完成";
				break;
			case '4':
				$value['status'] = "未通过";
				break;
			case '5':
				$value['status'] = "回收站";
				break;
			
		}
	}
    $this->assign("list",$list);
	$total   = DB::query("select count(*) as count from ims_xuan_zy_bankorder  WHERE 1 {$wheres} ")[0]['count'];
    $this->assign("count",$total);
    if(request()->isajax()){
    	echo json_encode($list);die;
    }
 	return	$this->fetch("admin/shenqing");




	}
	public function add(){
		$id = $_REQUEST['id'];
	$item = DB::query("SELECT * FROM ims_xuan_zy_bankorder WHERE id='{$id}'")[0];
    $order =DB::query("SELECT * FROM  ims_xuan_zy_bank WHERE id='{$item['bank_id']}' ")[0];
    $order['fy']=json_decode($order['fy'],1);
   	$this->assign("order",$order);
   	$this->assign("item",$item);

	if(request()->ispost()){
		$id = $_REQUEST['id'];
		$status = $_REQUEST['status'];
		if($item['status'] == 3){
			$this->error("已完成的无法修改","admin/shenqing/index");
		}
	 		if($status == 3){

	 			 $sql = "UPDATE ims_xuan_zy_bankorder set status = '{$status}' , update_time = NOW() where id = '{$id}'";
		         	DB::query($sql);
		          $sql = "select fy from ims_xuan_zy_bank where bankname = '{$item['bank_name']}'";
		          $money = json_decode(DB::query($sql)[0]['fy'],true)['sx'];
		          $time = date("Y-m-d H:i:s",time());
		          $content = "您的下级办理了{$item['bank_name']} 卡，您获得了{$money}元的提成奖励！已到账，请查收！<br>{$time}";
		          $sql = "insert into ims_xuan_zy_article(`title`,`lanmu`,`uid`,`content`,`level`,`yidu`,`create_time`,`uniacid`) value('奖励到账通知','-6','{$item['suid']}','{$content}',0,0,'{$time}',2)";
		         	DB::query($sql);
		          $sql = "UPDATE ims_mc_members set credit2 = credit2+{$money} where uid = {$item['suid']} ";
		         	DB::query($sql);
		      } else {
		      	 $sql = "UPDATE ims_xuan_zy_bankorder set status = '{$status}' , update_time = NOW() where id = '{$id}'";
		         	DB::query($sql);

		      }
		      $this->success("修改成功","admin/shenqing/index");die;
		
		}
		

 	return	$this->fetch("admin/shenqing_upd");
	}

}